<template>
  <HomePanel ref="target" title="新鲜好物" sub-title="新鲜出炉 品质靠谱">
    <template #right>
      <!-- 更多组件 需要导入注册 -->
      <xtx-more></xtx-more>
    </template>
    <template #default>
      <ul ref="pannel" class="goods-list">
        <li v-for="item in list" :key="item.id">
          <RouterLink to="/">
            <img v-imglazy="item.picture" alt="" />
            <p class="name">{{ item.name }}</p>
            <p class="price">&yen;{{ item.price }}</p>
          </RouterLink>
        </li>
      </ul>
    </template>
  </HomePanel>
</template>

<script>
import { findNew } from '@/api/home'
import { ref } from 'vue'
import HomePanel from './home-panel.vue'
// 导入监控元素或组件是否在视口
// import { useIntersectionObserver } from '@vueuse/core'
import { useObserve } from '@/hooks'

export default {
  components: {
    HomePanel
  },
  setup () {
    const list = ref([])
    const getList = async () => {
      const res = await findNew()
      // console.log(res)
      list.value = res
    }
    // 无脑发请求
    // getList()
    // 执行监控
    // const target = ref(null) // 被监控的元素
    // const { stop } = useIntersectionObserver(
    //   target,
    //   // 注意：被监控的元素target，进入和离开可视区，都会执行回调函
    //   ([{ isIntersecting }]) => {
    //     // 组件数据懒加载：在视口发请求=》只发一次
    //     console.log('新鲜好物是否在视口：', isIntersecting)
    //     if (isIntersecting) {
    //       // 在视口
    //       getList()
    //       // 关闭监控
    //       stop()
    //     }
    //   }
    // )
    // 执行数据懒加载
    const { target } = useObserve(getList)

    return { list, target }
  }
}
</script>

<style scoped lang='less'>
.goods-list {
  display: flex;
  justify-content: space-between;
  height: 406px;
  li {
    width: 306px;
    height: 406px;
    background: #f0f9f4;
    // 激活效果
    transition: all 0.5s;
    &:hover {
      transform: translate3d(0, -6px, 0);
      box-shadow: 0 6px 8px rgba(0, 0, 0, 0.2);
    }
    // end
    img {
      width: 306px;
      height: 306px;
    }
    p {
      font-size: 22px;
      padding-top: 12px;
      text-align: center;
      text-overflow: ellipsis;
      overflow: hidden;
      white-space: nowrap;
    }
    .price {
      color: @priceColor;
    }
  }
}
</style>
